var app = getApp();
// pages/user/shoucang.js
Page({
  data:{
    page:1,
    productData: [],
    goodsurl: app.d.goodsurl,
  },
  onLoad:function(options){
    this.loadProductData();
  },
  onShow:function(){
    // 页面显示
    //this.loadProductData();
  },
  removeFavorites:function(e){
    var that = this;
    var ccId = e.currentTarget.dataset.favId;

    wx.showModal({
      title: '提示',
      content: '你确认移除吗',
      success: function(res) {

        res.confirm && wx.request({
          url: app.d.hostUrl + 'wapi/favdelete.html',
          method:'post',
          data: {
            seesionid: app.globalData.userInfo.sessionId,
            openid: app.globalData.userInfo.openid,
            id: ccId,
          },
          header: {
            'Content-Type':  'application/x-www-form-urlencoded'
          },
          success: function (res) {
            //--init data
            var data = res.data;
            //todo
            if (data.ret == 0) {
              //删除页面中元素
              for (var i = 0; i < that.data.productData.length; i++) {
                if (that.data.productData[i]['goods_id'] == ccId){
                  that.data.productData.splice(i, 1);
                }
              }
              
              that.setData({
                productData: that.data.productData,
              });
              //that.loadProductDatas();
            }
          },
        });

      }
    });
  },
  loadProductData:function(){
    var that = this;
    wx.request({
      //url: app.d.hostUrl + '/ztb/productZBT/GetCollectCategoryList',
      //url: app.d.hostUrl + '/Api/User/collection_my',
      url: app.d.hostUrl + 'wapi/favlist.html',
      method:'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        pageindex: that.data.page,
        pagesize:100,
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data
        if(res.data.ret == 0){
            var data = res.data.list;
            //that.initProductData(data);
            that.setData({
              productData: that.data.productData.concat(data),
            });
          //endInitData
        } else {
          that.setData({
            productData: '',
          });
        }
        
        
      },
    });
  },
  loadProductDatas: function () {
    var that = this;
    wx.request({
      //url: app.d.hostUrl + '/ztb/productZBT/GetCollectCategoryList',
      //url: app.d.hostUrl + '/Api/User/collection_my',
      url: app.d.hostUrl + 'wapi/favlist.html',
      method: 'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        pageindex: that.data.page,
        pagesize: 100,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data
        if (res.data.ret == 0) {
          var data = res.data.list;
          //that.initProductData(data);
          that.setData({
            productData: that.data.productData.concat(data),
          });
          //endInitData
        } else {
          that.setData({
            productData: '',
          });
        }


      },
    });
  },
  initProductData: function (data) {
    if (data){
      for (var i = 0; i < data.length; i++) {
        //console.log(data[i]);
        var item = data[i];

        item.Price = item.now_price / 100;
        item.ImgUrl = item.goods_image;

      }
    }
    
  },
});